#include "serialportassistant.h"
#include "ui_serialportassistant.h"
#include <QtSerialPort/QSerialPortInfo>
#include <QMessagebox>

SerialPortAssistant::SerialPortAssistant(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::SerialPortAssistant)
{
    ui->setupUi(this);
    QStringList serialPortNum;

    serialPort = new QSerialPort(this);
    connect(serialPort,SIGNAL(readyRead()),this,SLOT(serialPortReadReady_slot()));

    foreach (const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
    {
        serialPortNum << info.portName();
    }
    ui->cbSerialNum->addItems(serialPortNum);
}

SerialPortAssistant::~SerialPortAssistant()
{
    delete ui;
}

void SerialPortAssistant::serialPortReadReady_slot()
{
    QString buf;
    buf = QString(serialPort->readAll());
    ui->textOutput->appendPlainText(buf);
}


void SerialPortAssistant::on_btOpenSerial_clicked()
{
    /* 点击打开 获取界面中的参数并初始化串口 */
    QSerialPort::BaudRate baudRate;
    QSerialPort::DataBits dataBits;
    QSerialPort::Parity parity;
    QSerialPort::StopBits stopBits;

    if(ui->cbBaudrates->currentText() == "4800") baudRate = QSerialPort::Baud4800;
    else if(ui->cbBaudrates->currentText() == "9600") baudRate = QSerialPort::Baud9600;
    else if(ui->cbBaudrates->currentText() == "115200") baudRate = QSerialPort::Baud115200;
    else baudRate = QSerialPort::UnknownBaud;

    if(ui->cbDataBits->currentText() == "5") dataBits = QSerialPort::Data5;
    else if(ui->cbDataBits->currentText() == "6") dataBits = QSerialPort::Data6;
    else if(ui->cbDataBits->currentText() == "7") dataBits = QSerialPort::Data7;
    else if(ui->cbDataBits->currentText() == "8") dataBits = QSerialPort::Data7;
    else dataBits = QSerialPort::UnknownDataBits;

    if(ui->cbStopBits->currentText() == "1") stopBits = QSerialPort::OneStop;
    else if(ui->cbStopBits->currentText() == "1.5") stopBits = QSerialPort::OneAndHalfStop;
    else if(ui->cbStopBits->currentText() == "2") stopBits = QSerialPort::TwoStop;
    else stopBits = QSerialPort::UnknownStopBits;

    if(ui->cbParityBits->currentText() == "None") parity = QSerialPort::NoParity;
    else if(ui->cbParityBits->currentText() == "Odd") parity = QSerialPort::OddParity;
    else if(ui->cbParityBits->currentText() == "Mark") parity = QSerialPort::MarkParity;
    else if(ui->cbParityBits->currentText() == "Even") parity = QSerialPort::EvenParity;
    else if(ui->cbParityBits->currentText() == "Space") parity = QSerialPort::SpaceParity;
    else parity = QSerialPort::UnknownParity;

    serialPort->setPortName(ui->cbSerialNum->currentText());
    serialPort->setBaudRate(baudRate);
    serialPort->setDataBits(dataBits);
    serialPort->setParity(parity);
    serialPort->setStopBits(stopBits);

    if(serialPort->open(QIODevice::ReadWrite) == true)
    {
        QMessageBox::information(this,"提示","打开成功");
    }
    else
    {
        QMessageBox::critical(this,"警告","打开失败");
    }
}

void SerialPortAssistant::on_btCloseSerial_clicked()
{
    /* 点击关闭 关闭串口 */
    serialPort->close();
}

void SerialPortAssistant::on_btSend_clicked()
{
    /* 点击发送 将输入文本框中的内容发送到串口 */
    char *buf;
    buf = ui->textInput->toPlainText().toLocal8Bit().data();
    serialPort->write(buf);
}
